package com.econ.powercloud.ems.service.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.econ.powercloud.ems.dal.entity.EmsProductLocationConfig;
import com.econ.powercloud.ems.dal.mapper.EmsProductLocationConfigMapper;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Repository;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author : zjf
 * @Description :
 * @Date : 2021/11/17 17:54
 * @Modified by : zjf
 */
@Repository
public class EmsProductLocationConfigDAO extends ServiceImpl<EmsProductLocationConfigMapper, EmsProductLocationConfig> {

    /**
     * 根据location获取数据
     * @param location: 位置信息
     * @return 第一个key:productKey,第二个key:moduleCode
     */
    public Map<String, Map<String,List<EmsProductLocationConfig>>> getMapByLocation(Integer location){
        Map<String,Map<String,List<EmsProductLocationConfig>>> result = Maps.newHashMap();
        LambdaQueryWrapper<EmsProductLocationConfig> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(EmsProductLocationConfig::getLocation,location);
        List<EmsProductLocationConfig> configList = this.baseMapper.selectList(lambdaQueryWrapper);
        if(configList == null || configList.isEmpty()){
            return null;
        }
        Map<String,List<EmsProductLocationConfig>> productMap = configList.stream().collect(Collectors.groupingBy(d->d.getProductKey()));
        productMap.forEach((k,v)->{
            result.put(k,v.stream().collect(Collectors.groupingBy(d->d.getModuleCode())));
        });
        return result;
    }


    public Map<String, Map<String,List<EmsProductLocationConfig>>> getMapByLocationList(List<Integer> location){
        Map<String,Map<String,List<EmsProductLocationConfig>>> result = Maps.newHashMap();
        LambdaQueryWrapper<EmsProductLocationConfig> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.in(EmsProductLocationConfig::getLocation,location);
        List<EmsProductLocationConfig> configList = this.baseMapper.selectList(lambdaQueryWrapper);
        if(configList == null || configList.isEmpty()){
            return null;
        }
        Map<String,List<EmsProductLocationConfig>> productMap = configList.stream().collect(Collectors.groupingBy(d->d.getProductKey()));
        productMap.forEach((k,v)->{
            result.put(k,v.stream().collect(Collectors.groupingBy(d->d.getModuleCode())));
        });
        return result;
    }

    public List<EmsProductLocationConfig> selectListByLocation(Integer location){
        LambdaQueryWrapper<EmsProductLocationConfig> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(EmsProductLocationConfig::getLocation,location);
        List<EmsProductLocationConfig> configList = this.baseMapper.selectList(lambdaQueryWrapper);
        return configList;
    }

    /**
     * 根据location获取数据(sorting仅限于一维排序)
     * @param location: 位置信息
     * @param virtual: 是否虚拟点号（非必填，默认查询全部）
     * @return 第一个key:location
     */
    public  Map<Integer, List<EmsProductLocationConfig>> getMapByLocationSort(List<Integer> location, Integer virtual){
        List<EmsProductLocationConfig> mapByLocationSort = this.baseMapper.getMapByLocationSort(location,virtual);
        return  mapByLocationSort.stream().collect(Collectors.groupingBy(d -> d.getLocation(), LinkedHashMap::new,Collectors.toList()));
    }

    /**
     * 根据locations获取数据
     * @param locations: 位置信息集合
     * @return 第一个key:productKey,第二个key:moduleCode
     */
    public Map<String, Map<String,List<EmsProductLocationConfig>>> getMapByLocations(List<Integer> locations){
        Map<String,Map<String,List<EmsProductLocationConfig>>> result = Maps.newHashMap();
        LambdaQueryWrapper<EmsProductLocationConfig> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.in(EmsProductLocationConfig::getLocation,locations);
        List<EmsProductLocationConfig> configList = this.baseMapper.selectList(lambdaQueryWrapper);
        Map<String,List<EmsProductLocationConfig>> productMap = configList.stream().collect(Collectors.groupingBy(d->d.getProductKey()));
        productMap.forEach((k,v)->{
            result.put(k,v.stream().collect(Collectors.groupingBy(d->d.getModuleCode())));
        });
        return result;
    }

    public List<EmsProductLocationConfig> selectByLocations(List<Integer> locations){
        LambdaQueryWrapper<EmsProductLocationConfig> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.in(EmsProductLocationConfig::getLocation,locations);
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }

    public int deleteByProductKeys(List<String> productKeys,Integer measureType){
        LambdaQueryWrapper<EmsProductLocationConfig> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.in(EmsProductLocationConfig::getProductKey,productKeys);
        lambdaQueryWrapper.eq(EmsProductLocationConfig::getMeasureType,measureType);
        return this.baseMapper.delete(lambdaQueryWrapper);
    }

}
